package com.salim.leetcode.$141;

public class LinkedListCycle {
    public boolean hasCycle(ListNode head) {
        if(head==null){
            return false;
        }
        ListNode slow = head;
        ListNode fast = head;
        while (slow.next!=null&& fast.next!=null){
            slow = slow.next;
            fast = fast.next;
            if(fast.next!=null){
                fast = fast.next;
                if(slow==fast){
                    return true;
                }
            }else{
                return false;
            }
        }
        return false;
    }

    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}
